package com.zc.edu.controller;


import com.zc.edu.entity.Chapter;
import com.zc.edu.entity.vo.ChapterVo;
import com.zc.edu.service.ChapterService;
import com.zc.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author cong
 * @since 2022-11-25
 */
@RestController
@RequestMapping("/eduService/chapter")
@CrossOrigin
public class ChapterController {

	@Autowired
	private ChapterService chapterService;

	@GetMapping("/getAllChapter/{courseId}")
	public R getAllChapter(@PathVariable String courseId) {
        List<ChapterVo> chapterVoList = chapterService.getAllChapter(courseId);
		return R.ok().data("chapterVideoTree", chapterVoList);
	}

	// 添加章节
	@PostMapping("/addChapter")
	public R addChapter(@RequestBody Chapter chapter) {
		chapterService.save(chapter);
		return R.ok();
	}

	// 查询章节信息
	@GetMapping("/getChapterById/{id}")
	public R getChapterById(@PathVariable String id) {
		Chapter chapter = chapterService.getById(id);
		return R.ok().data("chapter", chapter);
	}

	// 修改章节信息
	@PostMapping("/updateChapter")
	public R updateChapter(@RequestBody Chapter chapter) {
		chapterService.updateById(chapter);
		return R.ok();
	}

	// 删除章节信息
	@DeleteMapping("/deleteChapter/{id}")
    public R deleteChapter(@PathVariable String id) {
		chapterService.deleteChapter(id);
		return R.ok();
	}
}

